package scenario {
	
	/*
	 public static const SCENARIO_PORTO_DEBUG:XML = 
	 <Scenario id="003" description="Genova Porto - Ovest">
	 <Graph>
	 <Nodes>
	 <Node Id="n0" x="30" y="20" SwitchId="" />
	 <Node Id="n1" x="90" y="60" SwitchId="s0" />
	 <Node Id="n2" x="60" y="10" SwitchId="" />
	 </Nodes>
	 <Links>
	 <Link Id="lA0n0" group="A" nodeId="n0" isDefault="true" />
	 <Link Id="lB0n0" group="B" nodeId="n0" isDefault="true" />
	 <Link Id="lA0n1" group="A" nodeId="n1" isDefault="true" />
	 <Link Id="lA1n1" group="A" nodeId="n1" isDefault="false" />
	 <Link Id="lB0n1" group="B" nodeId="n1" isDefault="true" />
	 </Links>
	 <Arcs>
	 <Arc Id="a0" LinkAId="lA0n0" linkBId="lA0n1" oX="40" oY="80" />
	 </Arcs>
	 <Logics>
	 <Logic Id="c0">
	 <Arcs>
	 <Arc Id="a0" />
	 </Arcs>
	 <Switchs>
	 <Switch Id="s0" NodeId="n1" Group="A" Moved="false" />
	 </Switchs>
	 </Graph>
	 </Scenario>
	 */
	
	
	import flash.display.*;
	import flash.text.TextField;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.*;
	import flash.events.*;
	import mx.core.*;
	
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapMouseEvent;	
	import com.google.maps.MapType;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.overlays.*;
	
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	//import spark.components.Button;
	
	import flash.utils.Timer;
	
	import scenario.*;	
	import scenario.graph.*;
	import scenario.train.*;
	
	import win.Monitor;

	
	
	public class Scenario
	extends Map
	implements IScenarioElement {
		
		public static const SCENARIO_TOOL_ADD_NODE:String = "add_node";
		public static const SCENARIO_TOOL_REMOVE_NODE:String = "remove_node";
		public static const SCENARIO_TOOL_ADD_ARC:String = "add_arc";
		public static const SCENARIO_TOOL_REMOVE_ARC:String = "remove_arc";
		public static const SCENARIO_TOOL_ADD_LOGIC:String = "add_logic";
		public static const SCENARIO_TOOL_REMOVE_LOGIC:String = "remove_logic";
		
		public static const SCENARIO_PORTO_DEBUG:String = "prova";
		
		private var trains:Vector.<Train>;
		private var scenarioElements:Vector.<ScenarioElement>;
		private var graph:Graph;
		
		private var time:int;
		private var prevTime:int = 0;
		private var fps:int;
		private var fps_txt:TextField;
		
		private var mode:String;
		private var tool:String;
		
		public function Scenario() {
			
			super.key = "ABQIAAAAR83oH7SkcBjV9yFoov6AcBQuzAyE3A39Oi47WDCUmRkOTzQo3hS8XHG5w2kFB_je-t17fO6CNdG76w";	// localhost
			
			super.sensor = "true";
			super.percentWidth = 100;
			super.percentHeight = 100;
			
			super.addEventListener(MapEvent.MAP_READY, onScenarioReady);
			super.addEventListener(MapMouseEvent.CLICK, onScenarioClick);
		}
		
		
		
		public function getTag():String {
			return "SCENARIO";
		}
		
		public function getElementType():String {
			return "SCENARIO";
		}
		public function getMode():String {
			return mode;
		}

		public function render():void {
			for each(var se:IScenarioElement in scenarioElements) {
				se.render();
			}
		
		}
		
		public function getNewTag(type:String):String {
			var i:int = 0;


			while (true) {
				var found:Boolean = false;
				var tag:String = type;
				tag.concat(i);
				// controllo l'esistenza di un elemento con questo
				// tag
				for each(var se:IScenarioElement in scenarioElements) {
					if (se.getTag().toString() == tag) {
						// trovato, occorre nuovo id
						i++;
						found = true;
						break;
					}
				}
				if(!found)
					return tag;
			}

		
			return null;
		}
		
		
		private function onScenarioClick(event:MapMouseEvent):void {
			
			var latLng:LatLng = event.latLng;
			
			tool = SCENARIO_TOOL_ADD_NODE;
			
			switch (mode) {
				
				case Monitor.MONITOR_MODE_SIMULATION:
					break;
				
				case Monitor.MONITOR_MODE_EDITOR:
					{
						switch (tool) {
								
							case SCENARIO_TOOL_ADD_NODE:
								{
									addNode(latLng);
								}
								break;
													
							default:
								break;
						}
					}
					break;
				
				default:
					break;
			}
			

//			var m:Marker = new Marker(event.latLng, null);
//			marks.push(m);
			
		}
		
		
		private function addNode(latLng:LatLng):void {
			var node:Node = new Node(getNewTag(Node.NODE_TAG_TYPE), latLng);
			addOverlay(node);
			Rttm.root().log.trace("Creazione nodo\n");
			
			// avvio il render
			
		}
		
		private function addElement():void {
			
		}
		
		private function onScenarioReady(event:MapEvent):void {
			
			addControl(new MapTypeControl());
			setCenter(new com.google.maps.LatLng(44.4030, 8.9118), 19, com.google.maps.MapType.SATELLITE_MAP_TYPE);
			
			//startLng = new com.google.maps.LatLng(44.4030, 8.9118);
			
			/*
			 marks = new Vector.<Marker>();
			 elementsGraph = new Vector.<ScenarioElement>();
			 for (var i:int =0; i<20; i++) {
			 var m:Marker = new Marker(startLng, null);
			 marks.push(m);
			 addOverlay(m);
			 }
			 
			 
			 fps_txt = new TextField();
			 super.addChild(fps_txt);
			 
			 for (var i:int =0; i<20; i++) {
			 //var pippo:MapMarker = new MapMarker(new Array(new LatLng(44.4030, 8.9118), new LatLng(44.4031, 8.9110)));
			 var pippo:Arc = new Arc(new Array(new LatLng(44.4030, 8.9118), new LatLng(44.4031, 8.9110)));
			 addOverlay(pippo);
			 elementsGraph.push(pippo);
			 }
			 */
		}
		
		public function changeMode(mode:String):void {
			for each(var se:IScenarioElement in scenarioElements) {
				se.changeMode(mode);
			}
			// segnalo il cambiamento di stato in qualche modo
			this.mode = mode;
		}
		
		public override function unload():void {
			
			clearScenario();
			super.unload();
		}
		
		private function clearScenario():void {
			
			
		}
		public function loadScenario(data:String):void {
			
			clearScenario();
			
			// TODO debug
			data = SCENARIO_PORTO_DEBUG;
		}
		
		public function saveScenario():String {
			return SCENARIO_PORTO_DEBUG.toString();
		}
	}
	
}